package com.atzxh.collection.list;

import com.atzxh.collection.Book;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 小红同学
 * @create 2022-10-11-11:38
 * @description
 */
@SuppressWarnings({"all"})
public class List_Exer {

    public static void main(String[] args) {

        //  完成对list的Book对象按价格的由低到高排序
        List list = new ArrayList();

        list.add(new Book("三国","罗",88));
        list.add(new Book("水浒","施耐庵",48));
        list.add(new Book("红楼梦","曹雪芹",58));

        for (Object o: list) {
            System.out.println(o);
        }

        sort(list);

        for (Object o: list) {
            System.out.println(o);
        }

    }

    public static void sort(List list){

        int listSize = list.size();

        for (int i = 0; i < listSize -1; i++) {
            for (int j = 0; j < listSize -1 -i; j++) {
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j + 1);
                if (book1.getPrice() > book2.getPrice()){
                    list.set(j,book2);
                    list.set(j+1,book1);
                }
            }
        }

    }
}
